package com.gf.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.Orderr;
import com.gf.service.impl.OrderrServiceImpl;
import com.gf.utils.Const;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
这是订单页面处理抢单请求的servlet
* */

@WebServlet(name = "GetorderServlet",urlPatterns = "/getorder")
public class GetorderServlet extends HttpServlet {
    static OrderrServiceImpl orderimpl = null;
    static {
        orderimpl=new OrderrServiceImpl();


    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();

        String order_id = request.getParameter("order_id");
        //获取订单参数
        int selectorderbuyid = orderimpl.selectorderbuyid(order_id);
        //获取该订单号下单的用户id
        JSONObject backjson = new JSONObject();
        //新建json对象
        Integer userid = Integer.valueOf((Integer) request.getSession().getAttribute("userid"));
        if (userid==selectorderbuyid){
            //将session中的userid取出与查询出的id相比较如果相等就输出错误的信息
            response.setStatus(300);
            backjson.put("status", 300);
            backjson.put("message", Const.NOSELF);
            backjson.put("id", selectorderbuyid);
            backjson.put("ids", userid);
        } else {
            //如果不是就相当于要创建一个orderr
            Orderr orderr = new Orderr();
            orderr.setOrder_id(order_id);
            orderr.setSend_id(Integer.valueOf((Integer) request.getSession().getAttribute("userid")));
            //将订单编号存入为了能够匹配对应的订单
            //将userid存入就是为了填充send_id
            orderimpl.updateorderbysendid(orderr);
            //将order对象传入
            //设置正确的响应码和响应信息
            response.setStatus(200);
            backjson.put("status", 200);
            backjson.put("message", Const.GETSUCCESS);

        }
        //打印输出
        out.println(JSON.toJSON(backjson));
        out.flush();
        out.close();

    }
}
